package com.sbj.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbj.dto.req.ExtensionReq;
import com.sbj.dto.res.ExtensionRes;
import com.sbj.dto.res.InviterRes;
import com.sbj.dto.utils.AmountUtil;
import com.sbj.entity.Expend;
import com.sbj.entity.Extension;
import com.sbj.entity.Income;
import com.sbj.entity.Member;
import com.sbj.mapper.ExtensionMapper;
import com.sbj.service.ExtensionService;
import com.sbj.service.IExpendService;
import com.sbj.service.IIncomeService;
import com.sbj.service.IMemberService;
import com.zmh.framework.constants.Constants;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/11 10:10
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class ExtensionServiceImpl extends ServiceImpl<ExtensionMapper, Extension> implements ExtensionService {


    @Override
    public Page<ExtensionRes> lists(ExtensionReq extensionReq) {
        Page<Extension> page=new Page<>(extensionReq.getPageNum(),extensionReq.getPageSize());
        LambdaQueryWrapper<Extension> eq = Wrappers.lambdaQuery(Extension.class).eq(Extension::getType, extensionReq.getType());
        return Convert.convert(new TypeReference<Page<ExtensionRes>>() {},this.page(page,eq));
    }

    private final BeanSearcher beanSearcher;


    private  final IIncomeService incomeService;

    private final IMemberService memberService;


    @Override
    public void saveExtension(BigDecimal goodsPrice,Integer type) {
        Map<String, Object> build = MapUtils.builder().field("memberId", StpUtil.getLoginIdAsInt()).build();
        InviterRes inviterRes = beanSearcher.searchFirst(InviterRes.class, build);
        Extension extension = findOne(type);
        if(ObjectUtil.isNotNull(inviterRes) && ObjectUtil.isNotNull(extension)){
            Income expend = new Income();
            expend.setType(type);
            expend.setPrice(AmountUtil.scale(goodsPrice, extension.getScale()));
            expend.setMemberId(inviterRes.getId());
            expend.setName(extension.getName());
            expend.setSerialNum("C"+ IdUtil.nanoId(12));
            incomeService.save(expend);
            Member member = memberService.getById(inviterRes.getId());
            if(type.equals(Constants.ONE)){
                member.setExtension(NumberUtil.add(member.getExtension(),AmountUtil.scale(goodsPrice, extension.getScale())));
            }else if(type.equals(Constants.TWO)){
                member.setBonus(NumberUtil.add(member.getBonus(),AmountUtil.scale(goodsPrice, extension.getScale())));
            }
            this.getById(member);
        }

    }

    private Extension findOne(Integer type){
        List<Extension> list = this.list(Wrappers.lambdaQuery(Extension.class).eq(Extension::getType, type));
        if(ObjectUtil.isNotNull(list) && list.size()>0){
           return list.get(Constants.ZERO);
        }
        return null;
    }
}
